<% if object.async -%>
<%
  stat_path = python_literal(object.async.status.path.split('/'))
  op_path = python_literal(object.async.operation.path.split('/'))
  err_path = python_literal(object.async.error.path.split('/'))
  err_msg = object.async.error.message
-%>
<%=
  lines(emit_link('async_op_url', build_url(object.async_operation_url), object, true), 2)
-%>
def wait_for_operation(module, response):
    op_result = <%= method_call('return_if_object',
                                ['module', 'response',
                                 ("'#{object.async.operation.kind}'" if object.kind?)]) %>
    if op_result is None:
        return {}
    status = navigate_hash(op_result, <%= stat_path -%>)
<% if object.nested_query.nil? -%>
    wait_done = wait_for_completion(status, op_result, module)
<% if object.async.result.resource_inside_response -%>
    raise_if_errors(wait_done, <%= err_path -%>, module)
    return navigate_hash(wait_done, ['response'])
<% else #if object.async.result.resource_inside_response -%>
<% res_path = python_literal(object.async.result.path.split('/')) -%>
<% method = method_call('fetch_resource', ['module', "navigate_hash(wait_done, #{res_path})", (quote_string(object.kind) if object.kind?)]) -%>
<% if object.decoder? -%>
    response = <%= method %>
    if response:
        return <%= method_call('decode_response', ['response', 'module']) %>
    else:
        return {}
<% else -%>
    return <%= method -%>
<% end # if object.decoder? -%>
<% end #if object.async.result.resource_inside_response -%>
<% else # object.nested_query.nil? -%>
    wait_for_completion(status, op_result, module)
<%=
  obj_kind = quote_string(object.kind)
  indent(lines([
          "return fetch_wrapped_resource(",
          "module,",
          "#{obj_kind},",
          "'#{object.nested_query.kind}',",
          "'#{object.nested_query.keys.first}')"
        ].join("\n")
  ), 4)
-%>
<% end # object.nested_query.nil? -%>


def wait_for_completion(status, op_result, module):
<%
-%>
    op_id = navigate_hash(op_result, <%= op_path -%>)
<% if object.async.operation&.full_url -%>
    op_uri = navigate_hash(op_result, <%= python_literal(object.async.operation.full_url.split('/')) -%>)
<% elsif object&.custom_code&.custom_async_function -%>
    op_uri = <%= object&.custom_code&.custom_async_function -%>(module, {'op_id': op_id})
<% else -%>
    op_uri = async_op_url(module, {'op_id': op_id})
<% end -%>
<% if object.async.status.complete == true -%>
    while not status:
<% else -%>
    while status != <%= python_literal(object.async.status.complete) -%>:
<% end -%>
        raise_if_errors(op_result, <%= err_path -%>, module)
        time.sleep(<%= sprintf('%.1f', object.async.operation.wait_ms / 1000.0) %>)
        op_result = <%= method_call('fetch_resource', ['module', 'op_uri', ("'#{object.async.operation.kind}'" if object.kind?), 'False']) %>
        status = navigate_hash(op_result, <%= stat_path -%>)
    return op_result


def raise_if_errors(response, err_path, module):
    errors = navigate_hash(response, err_path)
    if errors is not None:
        module.fail_json(msg=errors)
<% end #if object.async -%>
